﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Eis51.CampanyCMS.Business.Bus;
using Eis51.CampanyCMS.FrameWork;
using Eis51.CampanyCMS.Model;
using Eis51.CampanyCMS.Model.Bus.QueryO;
using Eis51.CampanyCMS.Model.Bus;
using Eis51.Web.Admin;

namespace Eis51.CampanyCMS.WebUI.Admin.LeaveMsg 
{
    public partial class ViewMsg : BasePage
    {

        public int Mainid
        {
            get
            {
                if (Request.QueryString["MainId"] != null)
                {

                    return int.Parse(ComSafe.SafeValue(Request.QueryString["MainId"]));
                }
                if (ViewState["MainId"] != null)
                {
                    return int.Parse(ViewState["MainId"].ToString());
                }
                return 0;
            }
            set
            {
                ViewState["MainId"] = value;
            }
        }

        public string keyType
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["keyType"]))
                {

                    return Request.QueryString["keyType"];
                }
                return "";
            }
        }

        public string KeyValue
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["KeyValue"]))
                {

                    return Request.QueryString["KeyValue"];
                }
                return "";
            }
        }


        public int ParentId
        {
            get
            {
                if (Request.QueryString["ParentId"] != null)
                {

                    return int.Parse(ComSafe.SafeValue(Request.QueryString["ParentId"]));
                }
                return 0;
            }
        }
       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ShowData();
                //已阅，把子信息改为已阅！
                LeaveMsgMgntBLL.GetInstance().IsView(this.Mainid , AppContext.CurrentAdministrator.UserName);
                //Seller已阅该信息，主信息，的状态要改
                LeaveMsgMgntBLL.GetInstance().MainMsgIsViewed(this.Mainid, LeaveMsgHaveNew.SellerHaveNew);
            }
        }

        private void ShowData()
        {
            LeaveMsgMgntBLL lbll = LeaveMsgMgntBLL.GetInstance();
            LeaveMsgMgntQuery l = new LeaveMsgMgntQuery();
            l.parent_id = -1;
            l.Mainid = this.Mainid;

            DataTable dt = lbll.SearchData(l);
            rlist.DataSource = dt;
            rlist.DataBind();
        }
        private void ClearFace()
        {
            txtMsg_title.Text = "";
            txtMsg_Content.Text = "";
        }

        protected void btnExe_Click(object sender, EventArgs e)
        {
            string sellerid = AppContext.GetCurrentName();
            LeaveMsgMgntBLL lbll = LeaveMsgMgntBLL.GetInstance();
            LeaveMsgMgnt l = new LeaveMsgMgnt();
            l.CreateBy = sellerid;
            l.CreateDate = DateTime.Now;
            l.MsgTitle = txtMsg_title.Text;
            l.MsgContent = txtMsg_Content.Text;
            l.Ip = Request.Url.Host;
            l.KeyType = this.keyType;
            l.KeyValue = this.KeyValue;
            l.ReceiverId = LeaveMsgMgntBLL.GetInstance().GetBuyeridByValue(this.Mainid , sellerid);
            l.ParentId = this.ParentId;
            l.Mainid = this.Mainid;
            lbll.Submit(l);

            if (this.Mainid == 0)
            {
                this.Mainid = l.MsgId;
            }
            ShowData();
            ClearFace();
            //更新买家的有新消息的状态
            LeaveMsgMgntBLL.GetInstance().MainMsgIsNew(this.Mainid, LeaveMsgHaveNew.BuyerHaveNew);
            Response.Redirect("MsgList.aspx");
        }
    }
}
